package healthcaresystem;

import javax.swing.text.*;
/**
 * restrict the number of input for JTextFields
 * How to use: text2.setDocument(new LengthRestrictedDocument(8));
 */
public final class LengthRestrictedDocument extends PlainDocument {

  private final int limit;

  public LengthRestrictedDocument(int limit) {
    this.limit = limit;
  }

  @Override
  public void insertString(int offs, String str, AttributeSet a)
      throws BadLocationException {
    if (str == null)
      return;

    if ((getLength() + str.length()) <= limit) {
      super.insertString(offs, str, a);
    }
  }
}